home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 75 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  99 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.kei.com!wang!news
  3. From: emild@cs.technion.ac.il (Kohn Emil Dan)
  4. Subject: Re: help about structure (correction)
  5. Organization: Technion, Israel Institute of Technology
  6. Date: Sun, 31 Dec 1995 15:29:19 GMT
  7. Message-ID: <Pine.SV4.3.91-heb-2.04.951231172450.28898A-100000@cs.technion.ac.il>
  8. References: <4c3t9q$aob@chleuasme.francenet.fr> 
  9. Sender: news@wang.com
  10.  
  11.  
  12.  
  13.  
  14. Sorry, my last advice had a little flaw (I hope this is less bugful :-)
  15.  
  16.  
  17. On 30 Dec 1995, jo wrote:
  18.  
  19. > How can I translate from Pascal to C such a structure:
  20. > token=record;
  21. >   begin
  22. >        nature:tokennature;
  23. >        case nature : tokennature of
  24. >               tkinteger: (i:integer);
  25. >               tkreal: (r:double);
  26. >               ...
  27. >   end;
  28. > Can i use "switch" in a structure implementation ?
  29.  
  30. No, you can't.
  31.  
  32.  
  33. A suggestion would be something like:
  34.  
  35.  
  36. typedef enum {tkinteger,tkreal,...} tokennature;
  37.  
  38. struct token
  39. {
  40. tokennature nature;
  41.  
  42. union
  43.   {
  44.     int i;
  45.     double r;
  46.     .
  47.     .
  48.     .
  49.   }value;
  50. };
  51.  
  52.  
  53. And when you _USE_ the structure:
  54.  
  55.  
  56. .
  57. .
  58. .
  59. .
  60.  
  61. struct token token;
  62. .
  63. .
  64. .
  65.  
  66. switch (token.nature)
  67. {
  68.  
  69.    case tkinteger:
  70.  
  71.     token.value.i=4;
  72.     .
  73.     .
  74.     .
  75.     break; 
  76.  
  77.    case tkreal:
  78.  
  79.     token.value.d=2.5;
  80.     .
  81.     .
  82.     .
  83.     break;
  84.  
  85.  
  86.   .
  87.   .
  88.   .
  89. }
  90. .
  91. .
  92. .
  93.  
  94. Best regards, and a HAPPY NEW YEAR!!!
  95.  
  96.  
  97.                                 Emil
  98.